package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.system.dao.ReturnCodeRepository;
import com.ssy.lingxi.system.dto.request.ReturnCodeDeleteRequest;
import com.ssy.lingxi.system.entity.ReturnCode;
import com.ssy.lingxi.system.service.ReturnCodeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 返回码设置 管理
 */
@Service
public class ReturnCodeServiceImpl implements ReturnCodeService {


    @Resource
    ReturnCodeRepository returnCodeRepository;

    /**
     * 查询返回码设置列表
     *
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Override
    public Wrapper<PageData<ReturnCode>> returnCodeList(PageModel request) {
        Page<ReturnCode> all = returnCodeRepository.findAll(PageRequest.of(request.getCurrent(), request.getPageSize(), Sort.by("id").ascending()));
        List<ReturnCode> content = all.getContent();
        return Wrapper.success(new PageData<>(all.getTotalElements(),content));
    }

    /**
     * 删除返回码记录
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Transactional
    @Override
    public Wrapper returnCodeDelete(ReturnCodeDeleteRequest request) {
        returnCodeRepository.deleteById(request.getId());
        return Wrapper.success();
    }

    /**
     * 修改返回码状态
     *
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Transactional
    @Override
    public Wrapper returnCodeUpdate(ReturnCode request) {
        returnCodeRepository.save(request);
        return Wrapper.success();
    }

    /**
     * 添加返回码设置记录
     *
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Override
    public Wrapper returnCodeAdd(ReturnCode request) {
        returnCodeRepository.save(request);
        return Wrapper.success();
    }
}
